﻿using Qiniu.Storage;
using Qiniu.Util;

namespace Api
{
    [Route("")]
    public class QiniuController : AuthBaseController
    {
        [HttpGet]
        [Route("qiniu")]
        public QiniuModel Qiniu()
        {
            //账户下的AccessKey
            string AccessKey = AppSetting.Config.Qiniu.AK;

            //账户下的SecretKey
            string SecretKey = AppSetting.Config.Qiniu.SK;

            //对象存储里的空间名称
            string Bucket = AppSetting.Config.Qiniu.Bucket;

            //当前空间下配置的域名
            string Domain = AppSetting.Config.Qiniu.Domain;

            //当前空间所在的存储区域
            string BucketArea = AppSetting.Config.Qiniu.BucketArea;

            Mac mac = new Mac(AccessKey, SecretKey);

            PutPolicy putPolicy = new PutPolicy();
            putPolicy.Scope = Bucket;
            putPolicy.SetExpires(3600 * 24);//24小时

            return new QiniuModel
            {
                Token = Auth.CreateUploadToken(mac, putPolicy.ToJsonString()),
                Domain = Domain,
                BucketArea = BucketArea
            };
        }
    }
}
